package cn.dg.manager.goods.goodsClassify.action;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.druid.util.StringUtils;

import cn.dg.manager.base.BaseController;
import cn.dg.manager.base.entity.HttpResult;
import cn.dg.manager.base.util.PageData;
import cn.dg.manager.common.entity.PageEntity;
import cn.dg.manager.goods.goodsClassify.entity.GoodsType;
import cn.dg.manager.goods.goodsClassify.service.GoodsService;

@Controller
public class GoodsClassifyAction extends BaseController {

	@Autowired
	private GoodsService goodsService;
	
	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value="/goodsClassify/index")
	public String index() {
		return "goods/goodsClassify/main";
	}
	
	/**
	 * 获取商品分类的数据
	 */
	@RequestMapping(value="/goodsClassify/search", method = RequestMethod.POST)
	@ResponseBody
	public Object getGoodsTypeList() {
		PageData pd = this.getPageData();
		
		String page = pd.getString("page");
		String rows = pd.getString("rows");
		String typeName = pd.getString("typeName");
		// 组装查询参数
		Map<String, Object> mapParams = new HashMap<String, Object>(4);
		mapParams.put("page", page);
		mapParams.put("rows", rows);
		mapParams.put("typeName", typeName);
		
		PageEntity<GoodsType> result = goodsService.getGoodsTypeListPage(mapParams);
		return result;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/goods/goodsClassify/add")
	public ModelAndView add() {
		ModelAndView mv = this.getModelAndView();
		mv.addObject("mode", "0");
		mv.setViewName("goods/goodsClassify/detail");
		return mv;
	}
	
	/**
	 * 跳转至新增培训信息管理编辑窗口detail.jsp
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping(value="/goods/goodsClassify/update")
	public ModelAndView update() {
		ModelAndView mv = this.getModelAndView();
		// 更新
		mv.addObject("mode", "1");
		mv.setViewName("goods/goodsClassify/detail");
		return mv;
	}
	
	/**
	 * 保存
	 * 
	 * @return
	 */
	@RequestMapping(value="/goods/goodsClassify/saveOrUpdate")
	@ResponseBody
	public  Object saveOrUpdate () {
		PageData pd = this.getPageData();
		GoodsType bean = new GoodsType();
		String type_id = pd.getString("type_id");
		if(!StringUtils.isEmpty(type_id)) {
			bean.setTypeId(Integer.valueOf(type_id));
		}
		bean.setTypeAbbrName(pd.getString("type_abbr_name"));
		bean.setTypeName(pd.getString("type_name"));
		bean.setUpdateUserId(0L);
		
		long rslt = 0L;
		if(StringUtils.isEmpty(type_id)) {
			rslt = goodsService.insertGoodsType(bean);
		} else {
			rslt = goodsService.updateGoodsType(bean);
		}
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("创建商品分类失败！", "", "-1");
		}
	}
	
	/**
	 * 删除商品基本信息
	 * 
	 * @return
	 */
	@RequestMapping(value="/goods/goodsClassify/del")
	@ResponseBody
	public  Object delete () {
		PageData pd = this.getPageData();
		long rslt = goodsService.deleteGoodsType(Arrays.asList(pd.getString("type_id").split(",")));
		if(rslt > 0) {
			return HttpResult.createSuccessResult("", "0");
		} else {
			return HttpResult.createErrorResult("删除商品分类失败！", "", "-1");
		}
	}
}
